﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Cursos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnInserir_Click(object sender, ImageClickEventArgs e)
    {
        if (InsereCurso())
            Response.Redirect("mensagensc.aspx?id=1");
    }

    protected bool InsereCurso()
    {
        bool insere = false;
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand insereCurso = null;
        try
        {
            conexaoIntranet.Open();
            string instrucao = "INSERT INTO CamaraCursos (Curso, Descricao, DescricaoParcial, CodCategoria, CH) values ('" + txtCurso.Text + "', '" + txtDescricao.Text + "', '" + txtDescricaoParcial.Text + "', " + ddCodCategoria.Text + ", " + txtCH.Text + ")";
            insereCurso = new SqlCommand(instrucao, conexaoIntranet);
            insere = (insereCurso.ExecuteNonQuery() == 1);
        }
        catch (Exception erro)
        {
            lblMensagem.Text = erro.Message;
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return insere;
    }

    protected void ddCursosCadastrados_SelectedIndexChanged(object sender, EventArgs e)
    {
        LimpaControles();
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand consultaCurso = null;
        SqlDataReader drCurso;
        try
        {
            if (conexaoIntranet != null && conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();
            string instrucao = "Select * FROM CamaraCursos where CodCurso = " + ddCursosCadastrados.SelectedValue;
            consultaCurso = new SqlCommand(instrucao, conexaoIntranet);
            drCurso = consultaCurso.ExecuteReader();
            if (drCurso.HasRows)
            {
                drCurso.Read();
                txtCursoAlterar.Text = drCurso["Curso"].ToString();
                txtDescricaoAlterar.Text = drCurso["Descricao"].ToString(); ;
                txtDescricaoParcialAlterar.Text = drCurso["DescricaoParcial"].ToString(); ;
                ddCodCategoriaAlterar.SelectedValue = drCurso["codCategoria"].ToString();
                txtCHAlterar.Text = drCurso["ch"].ToString();
            }
            drCurso.Close();
        }
        finally
        {
            conexaoIntranet.Close();
        }

    }

    private void LimpaControles()
    {
        txtCursoAlterar.Text = "";
        txtDescricaoAlterar.Text = "";
        txtDescricaoParcialAlterar.Text = "";
        ddCodCategoriaAlterar.SelectedIndex = -1;
        txtCHAlterar.Text = "";
    }
    protected void btnAlterar_Click(object sender, ImageClickEventArgs e)
    {
        if (AlteraCurso())
            Response.Redirect("mensagensc.aspx?id=2");
    }

    protected bool AlteraCurso()
    {
        bool altera = false;
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand alteraCurso = null;
        try
        {
            conexaoIntranet.Open();
            string instrucao = "UPDATE CamaraCursos Set Curso = '"+ txtCursoAlterar.Text +"', Descricao = '"+ txtDescricaoAlterar.Text +"', DescricaoParcial = '"+ txtDescricaoParcialAlterar.Text +"', CodCategoria = "+ ddCodCategoriaAlterar.SelectedValue +", CH = "+ txtCHAlterar.Text +" WHERE CodCurso = " + ddCursosCadastrados.SelectedValue;
            alteraCurso = new SqlCommand(instrucao, conexaoIntranet);
            altera = (alteraCurso.ExecuteNonQuery() == 1);
        }
        catch (Exception erro)
        {
            lblMensagem.Text = erro.Message;
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return altera;
    }

}